//JavaScript代码区域
layui.use(['layer', 'form', 'laydate', 'table'], function() {
	var layer = layui.layer,
		$ = layui.$,
		form = layui.form,
		laydate = layui.laydate,
		table = layui.table;
	//监听提交
	form.on('submit(formDemo)', function(data) {
		table.reload('idTest', {
		  where: data.field
		});
		return false;
	});
	//执行一个laydate实例
	laydate.render({
		elem: '#start',
		max: 0
	});
	laydate.render({
		elem: '#end',
		max: 0
	});
	
	//数据表格实例
	table.render({
		
		id: 'idTest',
		elem: '#demo',
		url: IP+'/api/teacher_eav/sysCourse',
		height: 'full-180',
		method: 'get',
		page: true,
		even: true,
		cols: [[
		   {type:'checkbox'}
	      ,{field: 'cId',width:100, title: '课程编号', sort: true}
	      ,{field: 'cName', title: '课程名称'}
	      ,{field: 'cType', title: '类型'}
	      ,{field: 'createdTime', title: '创建时间', sort: true}
	      ,{field: 'updatedTime', title: '修改时间', sort: true}
	      ,{fixed: 'right', width:200, title: '操作', align:'center', toolbar: '#barDemo'}
	    ]],
	   
	    request: {
		  pageName: 'page'
		  ,limitName: 'pageSize'
		},              
	    response:{
	    	  statusName: 'code'
			  ,statusCode: 200
			  ,msgName: 'msg'
			  ,countName: 'count'
			  ,dataName: 'data'
	    },
	    done:function(res, curr, count){
			layer.closeAll('loading');
			refreshTableDict();
	    }
	});
	//数据表格监听工具条(查看、编辑、删除按钮)
	table.on('tool(test)', function(obj){
	  var data = obj.data;
	  var layEvent = obj.event;
	  if(layEvent === 'del'){
	      var ts=data.cId;
	      var uids=[];
	      uids.push(data.cId)
		  deleteByUids(ts,uids);
	  } else if(layEvent === 'edit'){
	    window.location="course_update.html?cId="+data.cId;	    
	  }
	});
	//复选框批量操作
	table.on('checkbox(test)', function(obj){
		var checkStatus = table.checkStatus('idTest');
		if(checkStatus.data.length>0){
			$('#deleteall').show()
		}else{
			$('#deleteall').hide()
		}
	});
	$(function() {
		//批量删除按钮
		$('#deleteall').click(function() {
		var checkStatus = table.checkStatus('idTest');
			if(checkStatus.data.length > 0) {
				var list = checkStatus.data;
				var uids=[];
				var ts="";
				$.each(list, function(idx,obj) {
					uids.push(obj.cId)
					ts+=" "+obj.cId
				});
				deleteByUids(ts,uids);
			}
		})
		//添加按钮事件
		$('#adduser').on('click',function(){
			window.location='course_add.html?type=add'
		})
	})
	//增加
	function add(){
		
	}
	//单、批量删除
	function deleteByUids(ts,uids){
		layer.confirm('确认删除编号:'+ts+"？",{icon:3}, function(index) {
			layer.close(index);
			$.ajax({
				url: IP + '/api/teacher_eav/sysCourse',
				type: "DELETE",
				data: JSON.stringify(uids),
				success: function(result, status, xhr) {
					layer.closeAll('loading');
					layer.msg(result.message, {
						icon: 1,
						time: 1000
					},function(){
						table.reload('idTest', {});
					});
				}
			});
		});
	}
	//更新表格字典信息
	function refreshTableDict(){
		var table_datas=$('#demo').siblings('.layui-form').children('.layui-table-box')
			.children('.layui-table-body').children('.layui-table').children('tbody').children('tr');
		
		
		$.each(table_datas, function(idx,obj) {
			//类别
			var cType=$(this).children("td[data-field='cType']").children('div');
			for (var i=0;i<cType.length;i++) {
				if(cType.html()=="1"){
					cType.html("必修");
					break;
				}else if(cType.html()=="2"){
					cType.html("选修");
					break;
				}else{
					cType.html("其他");
					break;
				}
			
			}
	
			
		});
	}
	//查询信息
	function ViewUserDetail(uid){
		layer.open({
		  type: 2,
		  skin:'layui-layer-molv',
		  area: ['600px', '340px'],
		  maxmin: true,
		  content: 'course_query.html?cId='+cId
		});
	}
});